package com.teamwings.ctrls.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teamwings.common.vo.Result;
import com.teamwings.ctrls.entity.OatAppSupervision;
import com.teamwings.ctrls.service.OatAppSupervisionService;
import com.teamwings.ctrls.vo.OatAppSupervisionAddVO;
import com.teamwings.ctrls.where.OatAppSupervisionWhere;
import com.teamwings.modules.head.entity.FormTableHead;
import com.teamwings.modules.head.service.IFormTableHeadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.zhyd.oauth.utils.StringUtils;
import org.jeecg.common.util.WhereUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * <p>
 * 督办管理控制器
 * </p>
 *
 * @author jobob
 * @since 2021-05-05
 */
@RestController
@Api(tags = "督办管理")
public class SupervisionController {

    @Autowired
    private OatAppSupervisionService service;
    @Autowired
    private IFormTableHeadService formTableHeadService;

    /**
     * 新增督办
     */
    @ApiOperation(value = "新增督办", notes = "")
    @PutMapping("/form/supervision")
    public Result<?> add(@Valid @RequestBody OatAppSupervisionAddVO supervision) {
        // 此处改为使用全局异常处理（AutoFormExceptionHandler），这样就不需要加BindingResult了
        // 获取表单名称
        String tableCname = formTableHeadService.getById(supervision.getFormHeadId()).getTableCname();
        OatAppSupervision oatAppSupervision = new OatAppSupervision();
        BeanUtils.copyProperties(supervision, oatAppSupervision);
        oatAppSupervision.setStatus(0)
                .setFormTableCname(tableCname);
        boolean b = service.save(oatAppSupervision);
        if (!b) {
            Result.error("新增失败");
        }
        return Result.OK("设置督办成功");
    }


    @ApiOperation(value = "获取督办设置", notes = "")
    @GetMapping("/form/supervisionDetail/{formInstId}")
    public Result<?> detail(@PathVariable("formInstId") String formInstId) {
        // QueryWrapper<OatAppSupervision> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<OatAppSupervision> queryWrapper = Wrappers.<OatAppSupervision>lambdaQuery();
        queryWrapper.eq(OatAppSupervision::getFormId, formInstId);
        OatAppSupervision record = service.getOne(queryWrapper);
        return Result.OK(record);
    }


    /**
     * 查询督办列表
     */
    @ApiOperation(value = "查询督办列表", notes = "")
    @GetMapping("/form/supervision")
    public Result<?> list(OatAppSupervisionWhere supervision) throws IllegalAccessException {

        QueryWrapper<OatAppSupervision> queryWrapper = new QueryWrapper<>();
        // 拼接查询条件
        WhereUtil.convertQuery(queryWrapper, supervision);
        // 如果有页码，则进行分页查询，默认每页 20 条，可传参修改
        if (supervision.getPageNo() != null) {
            Page<OatAppSupervision> page = new Page(supervision.getPageNo(), supervision.getPageSize());
            return Result.OK(service.page(page, queryWrapper));
        }
        // 没有侧进行普通查询
        return Result.OK(service.list(queryWrapper));
    }


}
